﻿using DevelopTool.Model.Enums;
using Sunny.UI;
using System;
using System.IO;

namespace DevelopTool.Common
{
    /// <summary>
    /// 写日志帮助类
    /// </summary>
    public static class LogHelper
    {
        private static object _errLogLock = new object();

        private static readonly string logDir = Path.Combine(GlobalData.AppDirectory, GlobalData.AppName_zh + "日志");


        /// <summary>
        /// 记录程序错误日志
        /// </summary>
        /// <param name="str"></param>
        public static void WriteErrLog(string content, bool sendMsg = true)
        {
            try
            {
                if (!Directory.Exists(logDir)) Directory.CreateDirectory(logDir);

                lock (_errLogLock)
                {
                    using (var sw = new StreamWriter($@"{logDir}\{DateTime.Now.DateString()}Error.log", true))
                    {
                        sw.WriteLine(content);
                        sw.WriteLine("------------------------------------------------------------------------------------------------------------------------");
                        sw.Close();
                    }
                }

                if (sendMsg && Setting.System.SendMsgModule.Contains("ErrorLog"))
                    FeiShuMsgHelper.SendCardMsgAsync(content, msgType: LogType.Error, contentType: "LOG");
            }
            catch (Exception ex)
            {

            }
        }


        public static void WriteErrLog(Exception error, bool sendMsg = true)
        {
            try
            {
                string strDateInfo = DateTime.Now.String() + "\r\n" + "出现应用程序未处理的异常：\r\n";
                string str = string.Format(strDateInfo + "异常类型：{0}\r\n异常消息：{1}\r\n异常信息：{2}\r\n",
                         error.GetType().Name, error.Message, error.StackTrace);

                if (!Directory.Exists(logDir)) Directory.CreateDirectory(logDir);

                lock (_errLogLock)
                {
                    using (var sw = new StreamWriter($@"{logDir}\{DateTime.Now.DateString()}Error.log", true))
                    {
                        sw.WriteLine(str);
                        sw.WriteLine("------------------------------------------------------------------------------------------------------------------------");
                        sw.Close();
                    }
                }

                if (sendMsg && Setting.System.SendMsgModule.Contains("ErrorLog"))
                    FeiShuMsgHelper.SendCardMsgAsync(str, msgType: LogType.Error, contentType: "LOG");
            }
            catch (Exception ex)
            {
            }
        }


        public static void WriteInfoLog(string content)
        {
            try
            {
                if (!Directory.Exists(logDir)) Directory.CreateDirectory(logDir);

                lock (_errLogLock)
                {
                    using (var sw = new StreamWriter($@"{logDir}\{DateTime.Now.DateString()}Info.log", true))
                    {
                        sw.WriteLine("时间：" + DateTime.Now.String());
                        sw.WriteLine(content);
                        sw.WriteLine("------------------------------------------------------------------------------------------------------------------------");
                        sw.Close();
                    }
                }
            }
            catch
            {
            }
        }
    }
}
